package com.wanli.storemobile.merchant;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.wanli.storemobile.R;
import com.wanli.storemobile.base.BaseFragment;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.AliPushBean;
import com.wanli.storemobile.bean.StoreReceivedBean;
import com.wanli.storemobile.bean.UserInfoBean;
import com.wanli.storemobile.homepage.model.HomePageModelImpl;
import com.wanli.storemobile.homepage.model.IHomePageModel;
import com.wanli.storemobile.mine.model.MineModelImpl;
import com.wanli.storemobile.utils.DateTimeUtil;
import com.wanli.storemobile.utils.NumberFormatUtils;
import com.wanli.storemobile.utils.SPManager;

import org.greenrobot.eventbus.Subscribe;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 首页 商户
 */
public class MerchantHomePageFragment extends BaseFragment {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.ll_order)
    LinearLayout llOrder;
    @BindView(R.id.ll_all_store)
    LinearLayout llAllStore;
    @BindView(R.id.ll_recharge_list)
    LinearLayout llRechargeList;
    @BindView(R.id.ll_recharge_config)
    LinearLayout llRechargeConfig;
    @BindView(R.id.tv_order_money)
    TextView tvOrderMoney;
    @BindView(R.id.ll_today_money)
    RelativeLayout llTodayMoney;
    private IHomePageModel homePageModel;
    private String start_time;
    private String end_time;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_merchant_home_page, container, false);
        ButterKnife.bind(this, view);
        registerEventBus();
        initView();
        requestMerchantMine();
        requestIncome();
        return view;
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Subscribe
    public void onAliPushBean(AliPushBean event) {
        requestIncome();
    }


    public void initView() {
        homePageModel = new HomePageModelImpl();
        start_time = DateTimeUtil.format(new Date());
        end_time = DateTimeUtil.format(new Date());
    }

    /**
     * 门店信息
     */
    public void requestMerchantMine() {
        new MineModelImpl().requestMerchantMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {
                SPManager.getInstance().saveUserInfoJson(new Gson().toJson(bean));
                tvName.setText(bean.getData().getName());
                tvMobile.setText(SPManager.getInstance().getUserMobile());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }


    private boolean isViewCreated; // 界面是否已创建完成
    private boolean isVisibleToUser; // 是否对用户可见

    /**
     * Fragment 创建期间setUserVisibleHint()方法是在onActivityCreated()之前被调用
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        tryLoadData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewCreated = true;
        tryLoadData();
    }

    public void tryLoadData() {
        if (isViewCreated && isVisibleToUser) {
            requestIncome();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        requestIncome();
    }

    /**
     * 收益
     */
    private void requestIncome() {
        homePageModel.requestMerchantReceived(start_time, end_time, new DataCallBack<StoreReceivedBean>() {
            @Override
            public void onSuccessful(StoreReceivedBean bean) {
                if (bean.getData() != null) {
                    if (!TextUtils.isEmpty(bean.getData().getPaid_order_fee())) {
                        String money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getPaid_order_fee()) / 100);
                        tvOrderMoney.setText(money);
                    } else {
                        tvOrderMoney.setText("0");
                    }
                }

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.ll_all_store, R.id.ll_order, R.id.ll_recharge_list, R.id.ll_recharge_config, R.id.ll_all_member, R.id.ll_today_money})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_all_store:
                skipToActivity(MerchantStoreListActivity.class);
                break;
            case R.id.ll_order:
                skipToActivity(MerchantOrderListActivity.class);
                break;
            case R.id.ll_recharge_list:
                skipToActivity(MerchantRechargeListActivity.class);
                break;
            case R.id.ll_recharge_config:
                skipToActivity(MerchantRechargeConfigActivity.class);
                break;
            case R.id.ll_today_money:
                skipToActivity(MerchantOrderListActivity.class);
                break;
            case R.id.ll_all_member:
                skipToActivity(MerchantMemberListActivity.class);
                break;
        }
    }
}
